// pages/pages-list/english/english.js
import api from '../../../utils/api/api'
import tool from "../../../utils/publics/tool"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      identity: 4, //身份 1专业观众 2普通观众 3媒体 4国外  
      real_name: '', // 姓名
      mobile: '', // 手机号码
      card_no: '', // 身份证
      company: '', // 公司单位组织  
      know: '', // 了解渠道 
      area:'',   // 地区
      code: '', // 兑换码
      pay_type: '', // 支付方式 1 免费支付  2兑换码支付 3直接支付  4拼团支付  
      qrcode_id: '', // 渠道码id  通过渠道码进来  
      pink_id: '', // 拼团id 通过拼团进来
      id: '', //活动id
    },
    verify: [
      'code',
      'pay_type',
      'qrcode_id',
      'pink_id',
      'id',
    ], // 校验
    channel: [{
      name: 'Brand',
      id: 1,
      close: false
    }, {
      name: 'Trader ',
      id: 2,
      close: false
    }, {
      name: 'Garage',
      id: 3,
      close: false
    }, {
      name: '4S Shop',
      id: 4,
      close: false
    }, {
      name: 'Hobbyist',
      id: 5,
      close: false
    }, {
      name: 'Car Owner',
      id: 6,
      close: false
    }, {
      name: 'Other',
      id: 7,
      close: false
    }],
    sltChannel: [], // 选中的渠道
    array:["Chinese Mainland","Hong Kong China","Macao China","Taiwan, China","Japan","Thailand"],    //ｚ地区
    showType: 2, // 弹框类型
    vShow: false, // 弹框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let info = this.data.info;
    let id = tool.storage("id");
    let pink_id =  tool.storage("pink_id")|| '';
    info.pink_id = pink_id;
    info.id = id;
    info.mobile = getApp().store.$state.userInfo.mobile || '';
    this.setData({
      info,
      pink_id
    })
  },
  // 输入框监听
  bindinput(e) {
    let key = e.currentTarget.dataset.name;
    let value = e.detail.value;
    let info = this.data.info;
    info[key] = value;
    this.setData({
      info
    })

  },
  // 选择器
  bindPickerChange: function(e) {
    let index=  e.detail.value;
    let info = this.data.info;
    info.area = this.data.array[index];
    this.setData({
      index,
      info
    })
  },
  // 多选
  channelFn(e) {
    let value = e.currentTarget.dataset.value;
    let index = e.currentTarget.dataset.index;
    let sltChannel = this.data.sltChannel;
    let channel = this.data.channel;
    let type = sltChannel.includes(value);
    if (type) {
      let num = sltChannel.findIndex(item => {
        return item == value
      });
      sltChannel.splice(num, 1);
      channel[index].close = false
    } else {
      sltChannel.push(value);
      channel[index].close = true
    };
    this.setData({
      sltChannel,
      channel
    })
  },
   // 关闭弹框
   close() {
    this.setData({
      vShow: false
    })
  },
  // 底部按钮点击
  pop(e) {
    let type = e.currentTarget.dataset.type;
    let info = this.data.info;
    let sltChannel = this.data.sltChannel;
    let showType = '';
    info.know = sltChannel.join(',');
    let verify = this.data.verify;
    for (var k in info) {
      if (!verify.includes(k)) {
        if (!info[k]) return tool.alert("请填写信息");
      }
    };
    if (type == 1) {
      showType = 2;
    } else if(type==2){
      showType = 1;
    }else{
      info.pay_type = 4;
      this.setData({
        info,
        showType:4,
        vShow:true
      })
      return
    }
    this.setData({
      showType,
      vShow: true,
      info
    })
  },
   //购买
   buy(e) {
    let type = e.detail.type;
    let code = e.detail.code || '';
    let info = this.data.info;
    let showType = this.data.showType;
    if (type == 1) { // 直接购买
      info.pay_type = 5;
    } else if (type == 2) { // 双人购买
      info.pay_type = 6;
      showType = 3;
    } else if (type == 3) { // 拼团购买
      info.pay_type = 4;
      // return tool.alert("该功能尚未开放")
    } else if (type == 4) { // 兑换码购买
      info.pay_type = 2;
      info.code = code;
    }
    this.setData({
      info,
      showType
    });
    if (type != 2) {
      this.submit();
    }
  },
  // 提交信息
  submit(e) {
    if (this.data.buttonClicked) return ;
    this.buttonClicked(this,500);
    console.log(this.data.info);
    let info  = this.data.info;
    info.qrcode_id = tool.storage("scene") || '';
    if (info.pay_type == 6) { // 双人票
      info = {
        ...info,
        ...e.detail.peer
      };
    }
    if (info.real_name == info.extra_name) {
      return tool.alert("请输入您同行人的姓名")
    } else if (info.mobile == info.extra_mobile){
      return tool.alert("请输入您同行人的电话号码")
    }else if (info.card_no == info.extra_card_no) {
      return tool.alert("请输入您同行人的身份证")
    }
    api.submit(info).then(res => {
      console.log(res);
      let {
        code,
        data,
        msg
      } = res;
      if (code == 1) {
        // 1 免费支付  2兑换码支付 3直接支付  4拼团支付 5单人票  6双人票    
        if (info.pay_type == 2) { // 兑换码支付
          tool.alert("报名成功", 1);
          // setTimeout(() => {
            tool.jump_rel(`/pages/pages-list/success/success?id=${data.enroll_id}`)
          // }, 400)
        } else if (info.pay_type == 3 || info.pay_type == 5 || info.pay_type == 6) { // 直接支付
          let js_config = data.js_config;
          wx.requestPayment({
            ...js_config,
            success(res) {
              tool.alert("报名成功", 1);
              // setTimeout(() => {
                tool.jump_rel(`/pages/pages-list/success/success?id=${data.enroll_id}`)
              // }, 400)
            },
            fail(res) {
              // setTimeout(() => {
                tool.jump_rel("/pages/pages-list/my-activity/my-activity")
              // }, 400)
            }
          })
        } else if (info.pay_type == 4) { // 拼团
          let js_config = data.js_config;
          wx.requestPayment({
            ...js_config,
            success(res) {
              // setTimeout(() => {
                tool.jump_rel(`/pages/pages-list/group-details/group-details?id=${data.pink_id}`)
              // }, 400)
            },
            fail(res) {
              // setTimeout(() => {
                tool.jump_rel("/pages/pages-list/my-activity/my-activity")
              // }, 400)
            }
          })
        }
      }
      // if (code == 1) {
      //   if(info.pay_type==2){   // 兑换码支付
      //     tool.alert("报名成功",1);
      //     setTimeout(() => {
      //       tool.jump_rel(`/pages/pages-list/success/success?id=${data.enroll_id}`)
      //     }, 1000)
      //   }else if(info.pay_type==3){    // 直接支付
      //     let js_config = data.js_config;
      //     wx.requestPayment({
      //       ...js_config,
      //       success(res) {
      //         tool.alert("报名成功",1);
      //         setTimeout(() => {
      //           tool.jump_rel(`/pages/pages-list/success/success?id=${data.enroll_id}`)
      //         }, 1000)
      //       },
      //       fail(res) {
      //         setTimeout(() => {
      //           tool.jump_rel("/pages/pages-list/my-activity/my-activity")
      //         }, 1000)
      //       }
      //     })
      //   }
        
      // }

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})